<template>
  <div class="main-wrap">
    <div class="operation-wrap">
      <el-button type="primary" size="mini" @click="openDia()">新增</el-button>
    </div>
    <el-table border :data="listData" :row-style="{ height: '40px' }" style="width: 100%">
      <el-table-column label="图书馆" prop="libraryName"></el-table-column>
      <el-table-column label="楼层名称" prop="floorName"></el-table-column>
      <el-table-column label="研讨室名称" prop="name"></el-table-column>
      <el-table-column label="负责人" prop="principal"></el-table-column>
      <el-table-column label="办公室" prop="office"></el-table-column>
      <el-table-column label="可约人数" prop="capacity"></el-table-column>
      <el-table-column label="联系方式" prop="phone"></el-table-column>
      <el-table-column label="联系邮箱" prop="email"></el-table-column>
      <el-table-column label="研讨室位置" prop="location"></el-table-column>
      <el-table-column label="设备" prop="equipment"></el-table-column>
      <el-table-column label="规则" prop="rule"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="openDia(scope.row)"> 编辑 </el-button>
          <el-button type="text" size="mini" @click="delRoom(scope.row)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="fromData.id ? '修改' : '新增'" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="fromData"
        status-icon
        ref="dataForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="图书馆" prop="libId">
          <el-select v-model="fromData.libId" @change="libChange">
            <el-option
              v-for="item in libraryList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼层名称" prop="floorId">
          <el-select v-model="fromData.floorId">
            <el-option v-for="item in floorList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="研讨室名称" prop="name">
          <el-input v-model="fromData.name"></el-input>
        </el-form-item>
        <el-form-item label="负责人" prop="principal">
          <el-input v-model="fromData.principal"></el-input>
        </el-form-item>
        <el-form-item label="办公室" prop="office">
          <el-input v-model="fromData.office"></el-input>
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="fromData.phone"></el-input>
        </el-form-item>
        <el-form-item label="联系邮箱" prop="email">
          <el-input v-model="fromData.email"></el-input>
        </el-form-item>
        <el-form-item label="可约人数" prop="capacity">
          <el-input v-model.number="fromData.capacity"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-input v-model="fromData.type"></el-input>
        </el-form-item>
        <el-form-item label="位置" prop="location">
          <el-input v-model="fromData.location"></el-input>
        </el-form-item>
        <el-form-item label="设备" prop="equipment">
          <el-input type="textarea" :rows="2" v-model="fromData.equipment"></el-input>
        </el-form-item>
        <el-form-item label="规则" prop="rule">
          <el-input type="textarea" :rows="2" v-model="fromData.rule"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/req'
export default {
  data() {
    return {
      listData: [],
      fileList: [],
      fromData: {
        id: '',
        name: '',
        principal: '',
        office: '',
        phone: '',
        email: '',
        capacity: '',
        libId: '',
        type: '',
        location: '',
        rule: '',
        floorId: '',
        equipment: '',
      },
      dialogVisible: false,
      libraryList: [],
      floorList: [],
    }
  },
  created() {
    this.getListData()
  },
  methods: {
    async getListData() {
      const res = await request.get('/seminarRoom/getSeminarRoomList')
      this.listData = res ? res : []
    },
    openDia(data) {
      this.getLibrary()
      if (data) {
        this.getFloor(data.libId)
      }
      this.dialogVisible = true
      this.fromData.id = ''
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (data) {
          Object.assign(this.fromData, data)
        }
      })
    },
    async getLibrary() {
      const res = await request.get('/library/getLibraryList')
      this.libraryList = res ? res : []
    },
    libChange(data) {
      this.fromData.floorId = ''
      this.getFloor(data)
    },
    async getFloor(libId = '') {
      const res = await request.get(`/floor/getFloorListByLibId?libId=${libId}`)
      this.floorList = res
    },
    async submitForm() {
      await request.post('/seminarRoom/submit', this.fromData)
      this.dialogVisible = false
      this.getListData()
    },
    delRoom(data) {
      request.post('/seminarRoom/delRoom', { id: data.id }).then((res) => {
        if (res.code == 200) {
          this.$message({
            type: 'success',
            message: '删除成功',
          })
          this.getListData()
        } else {
          this.$message({
            type: 'warning',
            message: res.msg,
          })
        }
      })
    },
  },
}
</script>

<style scoped>
:deep(.avatar-uploader .el-upload) {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
:deep(.avatar-uploader .el-upload:hover) {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
